// src/router/index.ts
import { createRouter, createWebHistory } from "vue-router";
import 仪表盘 from "../layout/仪表盘.vue";
import role from "../view/role/role.vue"
import menu from '../view/menu/menu.vue'
import Main from "../layout/main/main.vue"
import login from "../view/login/index.vue"
import Vi from "../view/vaccine/vaccineMassage.vue"
import VaccineDetail from "../view/vaccine/VaccineDetail.vue"
import VaccineForm from "../view/vaccine/VaccineForm.vue"
import add from "../view/vaccine/addVaccine.vue"
import stockList from '../view/stock/stockList.vue';
import InboundList from '../view/inbound/InboundList.vue'
import InboundDetail from '../view/inbound/InboundDetail.vue'
import InboundForm from '../view/inbound/InboundForm.vue'
import InboundStatistics from '../view/inbound/InboundStatistics.vue'
import OutboundList from "../view/outbound/OutboundList.vue";
import OutboundDetail from "../view/outbound/OutboundDetail.vue";
import OutboundStatistics from "../view/outbound/OutboundStatistics.vue";
import OutboundForm from "../view/outbound/OutboundForm.vue";
import StatisticsVaccine from '../view/statistics/StatisticsVaccine.vue'
const routes = [
  {
    path: "/", component: 仪表盘, redirect: '/Main', meta: { name: '首页', path: '/', requiresAuth: true },
    children: [{
      path: "role", component: role, name: 'Role', meta: { name: '角色管理', path: '/role' }
    },
    {
      path: "menu", component: menu, name: 'Menu', meta: { name: '疫苗信息管理', path: '/menu' }
    },
    {
      path: "main", component: Main, name: 'Main', meta: { name: '首页内容', path: '/Main', hideBreadcrumb: true } //路由元信息
    },
    {
      path: "Vi", component: Vi, name: 'Vi', meta: { name: '库存疫苗信息', path: '/Vi' },
      children: [{
        path: "VaccineDetail", component: VaccineDetail, name: 'VaccineDetail', meta: { name: '疫苗详情', path: '/Vi/VaccineDetail' },
        children: [{ path: "VaccineForm", component: VaccineForm, name: 'VaccineForm', meta: { name: '编辑疫苗', path: '/Vi/VaccineDetail/VaccineForm' } }]
      },
      ]
    },
    {
      path: "add", component: add, name: 'add', meta: { name: '新增疫苗', path: '/add' }
    },
    {
      path: "stockList", component: stockList, name: 'stockList', meta: { name: '库存列表', path: '/stockList' }
    },
    {
      path: "InboundList", component: InboundList, name: 'InboundList', meta: { name: '入库记录列表', path: '/InboundList' },
      children: [{
        path: "InboundDetail", component: InboundDetail, name: 'InboundDetail', meta: { name: '入库单详情', path: '/InboundList/InboundDetail' },
      }]
    },
    {
      path: "InboundForm", component: InboundForm, name: 'InboundForm', meta: { name: '新增入库', path: '/InboundForm' }
    },
    {
      path: "InboundStatistics", component: InboundStatistics, name: 'InboundStatistics', meta: { name: '今日入库统计', path: '/InboundStatistics' }
    },
    {
      path: "OutboundList", component: OutboundList, name: 'OutboundList', meta: { name: '出库记录列表', path: '/OutboundList' },
      children: [{
        path: "OutboundDetail", component: OutboundDetail, name: 'OutboundDetail', meta: { name: '出库单详情', path: '/OutboundList/OutboundDetail' },
      }]
    },
    {
      path: "OutboundStatistics", component: OutboundStatistics, name: 'OutboundStatistics', meta: { name: '今日出库统计', path: '/OutboundStatistics' }
    },
    {
      path: "OutboundForm", component: OutboundForm, name: 'OutboundForm', meta: { name: '新增出库', path: '/OutboundForm' }
    },
    {
      path: "StatisticsVaccine", component: StatisticsVaccine, name: 'StatisticsVaccine', meta: { name: '疫苗统计', path: '/StatisticsVaccine' }
    },

    ]
  },
  { path: "/login", component: login, name: login },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token');

  // 核心逻辑：已登录用户访问登录页跳转首页，未登录用户访问需授权页跳转登录页
  if (to.meta.requiresAuth) {
    // 需要登录的页面：有token则放行，无token跳转登录页
    token ? next() : next('/login');
  } else {
    // 登录页：已登录则跳转首页，未登录正常显示登录页
    if (to.path === '/login' && token) {
      next('/');
    } else {
      next();
    }
  }
});

export default router;
